package com.sysd.bean.enums;

import com.alibaba.fastjson2.JSONReader;
import com.sysd.bean.GroupMessage;
import com.sysd.bean.HeartMessage;
import com.sysd.bean.Message;
import com.sysd.entity.LoginUser;
import com.sysd.exception.ServiceException;
import com.sysd.functions.MessageParseFunction;
import lombok.AllArgsConstructor;

import java.util.Objects;

/**
 * @description:
 * @author: bsh
 * @date: 2025/6/12 15:21
 */
@AllArgsConstructor
public enum MessageTypeEnum {
    HEART(0, message -> message.toJavaObject(HeartMessage.class, JSONReader.Feature.UseDefaultConstructorAsPossible)),
    Auth(1, message -> message.toJavaObject(LoginUser.class, JSONReader.Feature.UseDefaultConstructorAsPossible)),
    MESSAGE(2, message -> message.toJavaObject(Message.class, JSONReader.Feature.UseDefaultConstructorAsPossible)),
    GROUP(3, message -> message.toJavaObject(GroupMessage.class, JSONReader.Feature.UseDefaultConstructorAsPossible)),
    ;

    private final Integer type;
    private final MessageParseFunction<Object> messageParseFunction;

    public static MessageParseFunction<Object> res(Integer type) {
        for (MessageTypeEnum messageTypeEnum : values()) {
            if (Objects.equals(messageTypeEnum.type, type)) {
                return messageTypeEnum.messageParseFunction;
            }
        }
        throw new ServiceException("找不到处理类!");
    }
}
